package cn.com.cabinet.manager.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cabinet.base.util.PageUtils;
import com.cabinet.easy.util.Page;
import com.cabinet.web.entity.Cabinet;
import com.cabinet.web.entity.CabinetApplication;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.github.miemiedev.mybatis.paginator.domain.Paginator;

import cn.com.cabinet.dao.CommoditiesClassDao;
import cn.com.cabinet.manager.dto.CabinetApplicationDto;
import cn.com.cabinet.service.CabinetApplicationServiceImpl;
import cn.com.cabinet.service.CabinetServiceImpl;
import cn.com.cabinet.service.SellerServiceImpl;
import cn.com.easy.dto.MessageDTO;
import cn.com.easy.dto.PageRequestParamDTO;
import cn.com.easy.utils.FastJSONUtils;
import cn.com.easy.utils.ResponseOutputUtils;

@Controller
@RequestMapping("/manager/cabinet/application")
public class CabinetApplicationController {
	
	@Autowired
	private CabinetApplicationServiceImpl cabinetApplicationServiceImpl;
	
	@Autowired
	private CabinetServiceImpl cabinetServiceImpl;
	
	@Autowired
	private SellerServiceImpl sellerServiceImpl;
	
	@RequestMapping()
	public String redirectApplication(){
		return "/cabinetApplication/index";
		
	}
	
	@RequestMapping("/list")
	public void getCabinetApplication(HttpServletRequest req, HttpServletResponse res, PageRequestParamDTO paramDto, Integer key, Integer status){
		try {
			int thispage = (paramDto.getPageNumber()-1)*10;
			
			List<CabinetApplication> cabinetList = cabinetApplicationServiceImpl.getCabinetApplicationByMark(status,thispage, paramDto.getPageSize());
			
			List<CabinetApplicationDto> cabinetApplicationDtoList = new ArrayList<>();
			CabinetApplicationDto c;
			for (CabinetApplication cabinetApplication : cabinetList) {
				c = new CabinetApplicationDto();
				c.setAddTime(cabinetApplication.getAddTime());
				c.setApplicationAddress(cabinetApplication.getcProvince()+cabinetApplication.getcCity()+cabinetApplication.getcArea());
				c.setcClass(cabinetServiceImpl.getCabinetByCid(cabinetApplication.getcId()).getcClass());
				c.setcId(cabinetApplication.getcId());
				c.setId(cabinetApplication.getId());
				c.setSellerId(cabinetApplication.getSellerId());
				c.setMark(cabinetApplication.getApplicationMark());
				c.setsTrueName(sellerServiceImpl.getSellerApplicationById(cabinetApplication.getSellerId()).getsTrueName());
				cabinetApplicationDtoList.add(c);
			}
			
			
			int totalcount = cabinetApplicationServiceImpl.getCabinetApplicationCountByMark(status);
			
			Paginator p = new Paginator(thispage, paramDto.getPageSize(), totalcount);
			PageList<CabinetApplicationDto> pageObject = new PageList<>(cabinetApplicationDtoList, p);
			Page<CabinetApplicationDto> page = PageUtils.getPage(pageObject);
			
			ResponseOutputUtils.renderJson(res, MessageDTO.newInstance("", true, page));
		} catch (Exception ex) {
			//logger.error("会员管理列表错误：", ex);
			ResponseOutputUtils.renderText(res, FastJSONUtils.toJsonString(MessageDTO.newInstance("", false, "会员管理列表错误")));
		}
	}
	
	
	/**
	 * 通过申请
	 * @return
	 */
	@RequestMapping("/success")
	@ResponseBody
	public String applicationsuccess(Long id){
		CabinetApplication cap = cabinetApplicationServiceImpl.getCabinetApplication(id);
		Cabinet ca = cabinetServiceImpl.getCabinetByCid(cap.getcId());
		if(ca.getSellerId() == null || ca.getSellerId().equals("") || ca.getSellerId().equals("null")){
			cap.setApplicationMark(1);
			cabinetApplicationServiceImpl.saveorUpdateCabinetApplication(cap);
			ca.setSellerId(cap.getSellerId());
			ca.setcMark(0);
			ca.setcProvince(cap.getcProvince());
			ca.setcCity(cap.getcCity());
			ca.setcArea(cap.getcArea());
			ca.setcLat(cap.getLat());
			ca.setcLng(cap.getLng());
			cabinetServiceImpl.addCabinet(ca);

			return "同意用户申请成功";
		}else{

			return "该智能柜已有其他商家通过了申请，请驳回用户申请！";
		}
		
		
	}
	
	/**
	 * 驳回申请
	 * @return
	 */
	@RequestMapping("/false")
	@ResponseBody
	public String applicationflase(Long id){
		CabinetApplication cap = cabinetApplicationServiceImpl.getCabinetApplication(id);
		cap.setApplicationMark(2);
		cabinetApplicationServiceImpl.saveorUpdateCabinetApplication(cap);
		
		
		return "驳回用户申请成功";
		
	}

}
